package org.summer.lawyer.mail;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.summer.common.mail.utils.MailUtils;
import org.summer.lawyer.domain.vo.spu.SpuVo;
import org.summer.lawyer.domain.vo.spuorder.SpuOrderVo;
import org.summer.lawyer.service.ISpuOrderService;
import org.summer.lawyer.service.ISpuService;
import org.summer.system.domain.vo.SysOssVo;
import org.summer.system.service.ISysOssService;

import java.io.File;

/**
 *
 */
@Slf4j
@Service
public class MailService {

    @Resource
    private ISpuOrderService spuOrderService;
    @Resource
    private ISpuService spuService;
    @Resource
    private ISysOssService sysOssService;
//    @Resource
//    private IMailLogService mailLogService;

//    @Async
//    @Override
//    public void onApplicationEvent(PaymentSuccessEvent event) {
//        sendMailBySpu(event.getPayOrderVo().getOrderId());
//    }

    /**
     * 发送商品附件到下单用户邮箱
     */
    public void sendMailBySpu(Long orderId) {
        //查询订单信息
        SpuOrderVo spuOrder = spuOrderService.selectSpuOrderVoById(orderId);
        //获取商品信息
        SpuVo spuVo = spuService.selectSpuVoById(spuOrder.getSpuId());
        if (StrUtil.isNotEmpty(spuOrder.getSpuFile())) {
            SysOssVo sysOssVo = sysOssService.getById(Long.parseLong(spuOrder.getSpuFile()));
            String successStatus = "1";
            String errorMessage = "";
            try {
                log.info("[onApplicationEvent][给用户({})发送附件({})][订单号：{}]", spuOrder.getUserName(), spuOrder.getSpuName(), orderId);
                //下载文件
                File file = FileUtil.file(FileUtil.getTmpDir()+ "/" + sysOssVo.getOriginalName());
                HttpUtil.downloadFileFromUrl(sysOssVo.getUrl(), file.getAbsolutePath());

                String subject = spuOrder.getSpuName();
                String body = spuVo.getSpuDesc();
                String recipientEmail = spuOrder.getEmail();
                MailUtils.sendText(recipientEmail, subject, body, file);
            } catch (Exception ex) {
                errorMessage = StrUtil.format("[onApplicationEvent][订单号:{}]发送邮件发生错误，{}", spuOrder.getOrderNumber(), ex.getMessage());
                log.error(errorMessage);
                successStatus = "2";

            }
        }else{
            log.info("[邮件发送][{}],未找到附件信息", spuOrder.getSpuName());
        }

        //记录日志--*****暂不记录---
//        MailLogBo mailLogBo = new MailLogBo();
//        mailLogBo.setRetryCount(0);
//        mailLogBo.setRecipientEmail(spuOrder.getEmail());
//        mailLogBo.setAttachmentId(String.valueOf(sysOssVo.getOssId()));
//        mailLogBo.setUrl(sysOssVo.getUrl());
//        mailLogBo.setSubject(spuVo.getSpuName());
//        mailLogBo.setBody(spuVo.getSpuDesc());
//        mailLogBo.setStatus(successStatus);
//        mailLogBo.setErrorMessage(errorMessage);
//        mailLogService.insertByBo(mailLogBo);
    }
}
